define(['./FormPlugin', 
	'../../BaseToolHandler',
	'../../Environment'], function(FormPlugin, BaseToolHandler, Environment) {
	'use strict';

	FormPlugin.factory('FormToolHandler', ['FormService', function (FormService) {	 	

        var _self = new BaseToolHandler('formTool');	

        _self.canActive = function(curToolName){
			return (curToolName == 'handTool');
		};
		
		function onCheckBoxClick(event) {
			//event.preventDefault();			   
			var scope = $(event.target).scope();
			scope.$evalAsync(function() {
				scope.formWidget.bCheck = !scope.formWidget.bCheck;
			});
		}

        function onFocus(event) {   
          // event.preventDefault();
           var scope = $(event.target).scope();                  
           scope.$evalAsync(function(){
                scope.formWidget.onfocus = true;
            });                                        
        }

		function onBlur(event) {			
			//event.preventDefault();
			var scope = $(event.target).scope();
			scope.$evalAsync(function() {
				scope.formWidget.onfocus = false;
			});
		}

		function onTextClick(event) {
			//event.preventDefault();
		}

		function onListItemClick(event) {		
			var scope = $(event.target).scope();
			var formWidget = scope.formWidget;						
			var bMultiple = !!formWidget.bMultiple;
			var thisOption = scope.option;
			var bSelected = !!thisOption.bSelected;
			var realValue = thisOption.realValue;

			if (!bMultiple && bSelected) {
				return;
			} 

			//多选
			if (bMultiple && event.ctrlKey){		
				var oldValue = FormService.getItemValue(formWidget.name);
				var oldValueArr =  oldValue.split(";");
				oldValueArr.push(realValue);
				FormService.setItemValue(formWidget.name, oldValueArr.join(";"));			
				return;
			}

			//非多选			
			FormService.setItemValue(formWidget.name, realValue);			
		}

		_self
		.on('click', '[form-check-box]', onCheckBoxClick)
		.on('focus', '[tabindex]', onFocus)
		.on('blur', '[tabindex]', onBlur)
		.on('click', '[form-text]', onTextClick)
		.on('click', '[form-list-box] [list-option]', onListItemClick);
		return _self;
	}]);
});